home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-08-07 | 4.3 KB | 177 lines | [TEXT/BROW] |
- /****
- * CNeoScrapStream.cp
- *
- * C++ implementation for NeoAccess abstract stream class.
- *
- * Copyright © Neologic Systems 1992-1994. All rights reserved
- *
- ****/
-
- #include "NeoTypes.h"
- #include "CNeoScrapStream.h"
- #include CNeoPersistH
-
- /* ****************************************************************** */
- /** Instance Methods **/
- /* ****************************************************************** */
- CNeoScrapStream::CNeoScrapStream(const OSType aType)
- {
- setType(aType);
- fOffset = 0;
- fData = NewHandle(0);
- NeoFailNil(fData);
- }
-
- CNeoScrapStream::~CNeoScrapStream(void)
- {
- flush();
- if (fData) {
- DisposHandle(fData);
- fData = nil;
- }
- }
-
- /*
- * Flush any buffered data.
- */
- #pragma segment NeoWrite
- void CNeoScrapStream::flush(void)
- {
- if (fOffset) {
- NeoAssert(fData);
- HLock(fData);
- PutScrap(fLength, fType, *fData);
- HUnlock(fData);
- fData = nil;
- fLength = 0;
- fOffset = 0;
- }
- else if (fData) {
- DisposHandle(fData);
- fData = nil;
- fLength = 0;
- }
- }
-
- /* ****************************************************************** */
- /** I/O Methods **/
- /* ****************************************************************** */
- /*
- * Position the stream to the given mark then allocate a memory block of the
- * given length then read that number of bytes into the block.
- */
- #pragma segment NeoStream
- NeoBlob CNeoScrapStream::readBlob(const NeoMark aMark, long &aLength, const NeoTag aTag)
- {
- long offset;
- NeoBlob blob;
-
- FailNIL(blob = (NeoBlob)NewHandle(0));
-
- aLength = GetScrap((Handle)blob, aTag, &offset);
- if (aLength < 0) { /* negative: error or no data in scrap */
- DisposHandle((Handle)blob);
- blob = nil;
- }
- else
- CNeoPersist::FCacheUsed += aLength;
-
- return blob;
- }
-
- /*
- * Read a chunk of data of length aLength into the buffer referred to by
- * aBuffer. This method assumes that the file is already positioned to
- * the proper location in the file.
- */
- #pragma segment NeoRead
- void CNeoScrapStream::readChunk(void *aBuffer, const long aLength, const NeoTag /* aTag */)
- {
- if (fOffset < fLength) {
- NeoAssert(fOffset + aLength <= fLength); // This shouldn't really happen, should it?
-
- NeoBlockMove((void *)((*(char **)fData) + fOffset), aBuffer, aLength);
- fOffset += aLength;
- }
- }
-
- /*
- * The process of reading an object from a stream sometimes needs to begin
- * by first configuring or otherwise preparing the stream. The readObject
- * method is overridden by those streams to perform these preamble tasks.
- */
- #pragma segment NeoStream
- void CNeoScrapStream::readObject(CNeoPersist *aObject, const NeoTag aTag)
- {
- Boolean dispose = (Boolean)(fData == nil);
- long length;
- long offset;
-
- if (!fData) {
- NeoFailNil(fData = NewHandle(0));
- fOffset = 0;
- fLength = 0;
- }
-
- length = GetScrap(fData, fType, &offset);
- if (length < 0) { /* negative: error or no data in scrap */
- if (dispose) {
- DisposHandle(fData);
- fData = nil;
- fOffset = 0;
- fLength = 0;
- }
- }
- else
- fLength = length;
- }
-
- /*
- * Position the stream to the given mark then write that number of
- * bytes to the stream.
- */
- #pragma segment NeoStream
- void CNeoScrapStream::writeBlob(NeoBlob aBlob, const NeoMark aMark, const long aLength, const NeoTag aTag)
- {
- if (aBlob) { /* blob exists */
- HLockHi((Handle)aBlob);
- PutScrap(aLength, aTag, NeoBlobGetPtr(aBlob));
- HUnlock((Handle)aBlob);
- }
- }
-
- /*
- * Write to the current location in the file a chunk of data of length
- * aLength taken from the buffer referred to by aBuffer. This method
- * assumes that the file is open and positioned to the proper location
- * in the file.
- */
- #pragma segment NeoWrite
- void CNeoScrapStream::writeChunk(const void *aBuffer, const long aLength, const NeoTag /* aTag */)
- {
- if (fData)
- SetHandleSize(fData, fOffset + aLength);
- else {
- NeoFailNil(fData = NewHandle(aLength));
- fOffset = 0;
- }
- fLength += aLength;
-
- NeoBlockMove(aBuffer, ((*(char **)fData) + fOffset), aLength);
- fOffset = fLength;
- }
-
- /*
- * The process of writing an object to a stream sometimes involves first
- * configuring or otherwise preparing the stream. The writeObject method
- * overridden by those streams to perform these preamble tasks.
- */
- #pragma segment NeoStream
- void CNeoScrapStream::writeObject(CNeoPersist *aObject, const NeoTag aTag)
- {
- ZeroScrap(); // Clear the previous contents of the scrap
- fLength = 0;
- fOffset = 0;
- }
-
-